<%-- 
    Document   : Pantalla_reset_pass
    Created on : 28-nov-2013, 11:35:33
    Author     : Roberto
--%>

<%@page import="java.util.GregorianCalendar"%>
<%@page import="GestorBDatos.Reset"%>
<%@page import="GestorBDatos.ResetJpaController"%>
<%@page import="GestorBDatos.UsuarioJpaController"%>
<%@page import="javax.persistence.Persistence"%>
<%@page import="javax.persistence.EntityManagerFactory"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html lang="es">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Seguridad informatica</title>
        <script language="JavaScript" src="js/datosajax.js" type="text/javascript" ></script>
        <link rel="stylesheet" href="css/bootstrap.css">
        <link rel="stylesheet" href="css/reset.css">
    </head>
    <body>
        <%
            if (request.getParameter("url") != null && request.getParameter("login") != null) {
                EntityManagerFactory emf = Persistence.createEntityManagerFactory("Seguridad_InformaticaPU");
                ResetJpaController resetController = new ResetJpaController(emf);

                
                String login = request.getParameter("login");
                session.setAttribute("login", login);
                Reset resetURL = resetController.findReset(login);
                if (resetURL != null) {

                    String url = request.getParameter("url");
                    GregorianCalendar fechaActual = new GregorianCalendar();
                    GregorianCalendar fechaURL = new GregorianCalendar();
                    fechaURL.setTime(resetURL.getHora());
                    long diff = fechaActual.getTimeInMillis() - fechaURL.getTimeInMillis();
                    // calcular la diferencia en horas
                    long diffHours = diff / (60 * 60 * 1000);
                    if (url.equals(resetURL.getUrl()) && diffHours < 1) {
                        session.setAttribute("url", url);
        %>
        
        <div class="container">

            <h1>Reset Password</h1>
            
            <form class="col-sm-3 form-reset" role="form" method="post" action="Procesar_reset.jsp">
                <input type="password" name="pass" class="form-control" placeholder="Contraseña nueva" id="pass" onkeyup="recogerPuntuacion(this.value,celda1,puntuacion,seguridad)">
                <div class="progress espacio">
                    <div  style="width: 0%" id="celda1"></div>
                </div>
                <span id="seguridad"></span>  
                <input type="hidden" name="puntuacion" id="puntuacion">
                <input type="password" name="control_pass" class="form-control" placeholder="Introducir de nuevo la contraseña" id="control_pass" >
            <%                
                if (request.getParameter("error") != null) {
                    if (request.getParameter("error").equals("1")) {
            %>
            <span class="text-danger">Las contraseñas no coinciden.</span>
            <%
                    } else if (request.getParameter("error").equals("2")) {
            %>
            <span class="text-danger">No has introducido todos los parametros.</span>  
            <%
                    } else if (request.getParameter("error").equals("3")) {
            %>
            <span class="text-danger">El nivel de seguridad es bajo,segurate de que has introducido 6 caracteres.</span>  
            <%
                    }
                }
            %>
                <button class="btn btn-lg btn-primary btn-block" type="submit">Enviar</button>
            </form>
                
        </div>
                
                
                
                <%
                    } else {
                        response.sendRedirect("Pantalla_Url_Invalida.jsp");
                    }
                } else {
                    response.sendRedirect("Pantalla_Url_Invalida.jsp");
                }
            } else {
                response.sendRedirect("Pantalla_Url_Invalida.jsp");
            }
        %>
        
    </body>
</html>
